﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace MetaSharp.Transformation.Tests.Patterns
{
	public class ProductionTests
	{
		[Fact]
		public async Task ProjectsSuccessfully()
		{
			var p = Pattern.Production(
                Pattern.Variable("c", Pattern.Any()),
                (char c) => 'z');
            var m = await Pattern.Match(p, "a");
			Assert.Equal('z', m.Value);
		}

		[Fact]
		public async Task DoesNotProjectWhenFails()
		{
			var projected = false;
			var p = Pattern.Production(
                Pattern.Not(Pattern.Any()),
                (char c) => projected = true);
            var m = await Pattern.Match(p, "a");

			Assert.False(projected);
		}

		[Fact]
		public async Task ProductionWithSingleParameterSucceeds()
		{
			var projected = false;
            var p = Pattern.Production(
                Pattern.Variable("c", Pattern.Any()),
                (char c) => projected = true);
            var m = await Pattern.Match(p, "a");

			Assert.True(projected);
		}

		[Fact]
		public async Task ProductionWithMutipleParametersSucceeds()
		{
			var p = Pattern.Production(
                Pattern.And(
                    Pattern.Variable("x", Pattern.Any()),
                    Pattern.Variable("y", Pattern.Any())),
                (char x, char y) => "xy=" + x + y);
            var m = await Pattern.Match(p, "ab");

			Assert.Equal("xy=ab", m.Value);
		}

		[Fact]
		public async Task ProductionsOfDifferentTypesIntoProductionWithMutipleParametersSucceeds()
		{
            // x:any y:any -> x + y
			var p = Pattern.Production(
                    Pattern.And(
                        Pattern.Variable("x", Pattern.Any()),
                        Pattern.Variable("y", Pattern.Any())),
                    (string x, int y) => x + y);
            var m = await Pattern.Match(p, new object[] { "hello", 123 });
			Assert.Equal("hello123", m.Value);
		}
	}
}
